#!/bin/csh -f
# jfd@Tue May 12 15:13:11 WDT 1998
#
# Shell script to convert AQUA distance restraint files using aq2x
# The results will be shown as links in the overview file.
#
# The variable 'aquaroot' must exist and point to the Aqua top directory.
# The 'aqsetup' script should reside in that directory.
#

if ( $#argv != 4 ) goto usage
goto begin

#####################
# usage block
#####################
usage:
    echo 'usage: aq2x.csh dir_numb output_format name_format terseness'
    echo ''
    echo '   dir_numb:              number of directory at the server side'
    echo '   output_format:         aqua nmrstar xplor diana biosym'
    echo '   name_format:           aqua disgeo disman xplor diana biosym'
    echo '   terseness:             0 through 9'

    goto ending

#####################
# initialization
#####################
begin:
# SAME AS IN PERL SCRIPTS, MANY ARE REDUNDANT TO THIS PROGRAM

# SITE DEPENDANT
    set serv_loc        = "AQUA_LOC/Aqua/server/"
    set serv_dir        = "AQUA_DIR/Aqua/server/"

    set cgi_bin_loc     = "CGI_LOC/Aqua/"
    set cgi_bin_dir     = "CGI_DIR/Aqua/"

    set tmp_loc         = "TMP_LOC/Aqua/"
    set tmp_dir         = "TMP_DIR/Aqua/"

    set webmaster       = "WEBMASTER"

# SITE INDEPENDENT
    set author          = "jurgenfd@gmail.com"

    set overview        = "overview.html"
    set logfile         = "server.log"
    set debug           = "false"

    setenv aquaroot $serv_dir/..
    if ( $debug == "true" ) then
        echo 'Aqua program directory:' $aquaroot
    endif
    source $aquaroot/aqsetup

    umask 0000              # File creation mask
    limit cputime     15    # Maximum number of seconds the CPU can spend
                            # on any single process spawned.
                            # This should be half the $cal_time_secs
                            # defined in the calling cgi script.
    limit filesize   100m   # Maximum size of any one file
    limit datasize   100m   # Maximum size of data (including stack)
    limit coredumpsize 0    # Maximum size of core dump file


    echo 'DETAILS CAN BE FOUND IN THE LOG FILES: aq2x.log AND aqpc.log'
    echo ' '
    echo 'AQ2X - Converting AQUA distance restraints'

    set path = ($path /usr/ruuci11/people/ruu/bin /usr/share/bin /usr/sbin \
                  $cgi_bin_dir )
    

#####################
# process the options
#####################
options:

set output_format = $2

switch ( "$3" )
    case "aqua":
	set name_format = ""
	breaksw
    case "disgeo":
	set name_format = "-n disgeo"
	breaksw
    case "disman":
	set name_format = "-n disman"
	breaksw
    case "xplor":
	set name_format = "-n xplor"
	breaksw
    case "diana":
	set name_format = "-n diana"
	breaksw
    case "biosym":
	set name_format = "-n biosym"
	breaksw
    default:
        goto usage
endsw
    
set terseness = $4

# go to the right dir
    set dir = $tmp_dir/$1

    if ( !(-e $dir ) )then
	echo "Directory $dir doesn't exist"
        goto ending
    endif
    
    cd $dir
    
# set restraint file
    set proj  = "restraints"
        
# convert uploaded dos file to unix. Macs might go wrong here?
    gawk '{gsub (/\015/,"",$0);print}' restraints > restraints.tmp
    \mv restraints.tmp restraints.pdb
    gawk '{gsub (/\015/,"",$0);print}' coordinates > coordinates.tmp
    \mv coordinates.tmp coordinates
        
# run AQ2X
    echo ''
    echo ''
    echo ''
    echo 'AQ2X - Running AQ2X itself'

    if ( $debug == "true" ) then
        echo 'executing:'
        echo "\
        aq2x $name_format -terseness $terseness $output_format restraints converted_distances coordinates"
    endif
    
    aq2x $name_format -terseness $terseness $output_format restraints converted_distances coordinates
    
    if ( -e converted_distances ) then
	set result = "true"
    else
	set result = "false"
	echo 'AQ2X - ERROR no file created'
    endif
    
ending:
#   Update the result file

    if ( ! $?result ) then
        set result = "false"
    endif

    echo 'AQ2X - result is' $result
    
    echo 'Finished conversion'

    touch finished # This file will be checked for by the perl script
    
    exit(0)
